package cn.dkdoo.mongodb.config;

import cn.dkdoo.utils.templateDefault.mongo.DefaultMongoTemplate;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

/**
 * @author nicky
 * @date 2021/5/12 下午2:18
 */
@Configuration
public class MongodbConfig {
    @Value("${spring.data.mongodb.test.uri}")
    private String testMongoUrl;

    public static final String defaultMongoTemplate = "defaultMongoTemplate";

    @Bean(name = defaultMongoTemplate)
    public DefaultMongoTemplate initDefaultMongoTemplate() throws Exception {
        return new DefaultMongoTemplate(this.template(), defaultMongoTemplate);
    }


    @Bean
    public MongoDbFactory dbMMBAppFactory() throws Exception {
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClientURI(testMongoUrl));
        return mongoDbFactory;
    }

    @Bean
    public MongoTemplate template() throws Exception {
        return new MongoTemplate(this.dbMMBAppFactory());
    }
}
